package es.bwain.xprt.action;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.SAXException;

public class BaseServlet extends HttpServlet {

	private static final long serialVersionUID = -6558372890329971464L;

	private String servletName;
	private String xprtClass;
	private String xsltFile;
	private String servletMapping;
	
	@Override
	public void init() throws ServletException {
		this.servletName = this.getServletName();
		
		InputStream configFile = null;
		try {
			configFile = this.getClass().getResourceAsStream(this.getServletConfig().getInitParameter("config"));
			Initializer.initProperties(configFile);
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			try {
				configFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		Digester digester = new Digester();
		digester.push(this);
		digester.setNamespaceAware(true);
		digester.setValidating(false);
		
		digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2);
		digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
		digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);

		InputStream input = this.getServletContext().getResourceAsStream("/WEB-INF/web.xml");
		
		try {
			digester.parse(input);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
	}
	
	public void addServletMapping(String servletName, String urlPattern) {
		if (servletName == null) {
			return;
		}
		if (servletName.equals(this.servletName)) {
			this.servletMapping = urlPattern;
		}
	}
	
	private String getFullPath(HttpServletRequest request, String mainPackage, boolean capitalize) {
		String uri = request.getRequestURI().substring(request.getContextPath().length());

		String auxClass = "";
		if (this.servletMapping.startsWith("*")){
			auxClass = uri.substring(1, uri.length() - this.servletMapping.length() + 1).replaceAll("/", "."); 
		} else if (this.servletMapping.endsWith("*")){
			auxClass = uri.substring(this.servletMapping.length() - 1).replaceAll("/", ".");
		}
		
		String[] theClass = auxClass.split("\\.");
		if (capitalize) {
			theClass[theClass.length-1] = StringUtils.capitalize(theClass[theClass.length-1]);
		} else {
			theClass[theClass.length-1] = StringUtils.uncapitalize(theClass[theClass.length-1]);
		}
		return mainPackage + "." + StringUtils.join(theClass, ".");
	}
	
	public void initVariables(HttpServletRequest request) {
		this.xprtClass = this.getFullPath(request, Initializer.getParameter(Initializer.XPRT_ACTION_PACKAGE), true) + "Action";
		
		StringBuffer sb = new StringBuffer();
		sb.append(Initializer.getParameter(Initializer.XPRT_XSLT_PROTOCOL));
		sb.append("://");
		sb.append(Initializer.getParameter(Initializer.XPRT_XSLT_HOST));
		if (!"80".equals(Initializer.getParameter(Initializer.XPRT_XSLT_PORT))){
			sb.append(":");
			sb.append(Initializer.getParameter(Initializer.XPRT_XSLT_PORT));
		}
		if ("true".equalsIgnoreCase(Initializer.getParameter(Initializer.XPRT_XSLT_USECONTEXTPATH))){
			sb.append(request.getContextPath());
		}
		sb.append(this.getFullPath(request, Initializer.getParameter(Initializer.XPRT_XSLT_FOLDER), false).replace(".", "/"));
		sb.append(Initializer.getParameter(Initializer.XPRT_XSLT_EXTENSION));
		this.xsltFile = sb.toString();
	}

	public String getXprtClass() {
		return xprtClass;
	}

	public String getXsltFile() {
		return xsltFile;
	}
	
	public String getServletMapping() {
		return servletMapping;
	}
	
}
